package com.example.bbs.service.impl;


import com.example.bbs.dao.PostDao;
import com.example.bbs.dao.ReplyDao;
import com.example.bbs.dao.impl.PostDaoImpl;
import com.example.bbs.dao.impl.ReplyDaoImpl;
import com.example.bbs.model.Reply;
import com.example.bbs.service.ReplyService;
import com.example.bbs.utils.TimeUtil;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * @auther: zhangxin
 * @Description:
 */
public class ReplyServiceImpl implements ReplyService {


    ReplyDao replyDao =new ReplyDaoImpl();
    PostDao postDao = new PostDaoImpl();
    @Override
    public int getCountByPostId(int postId) {

        List<Reply> replys = replyDao.findByPostId(postId);

        return replys.size();
    }

    @Override
    public List<Reply> getReplyList(int postId) {
        return replyDao.findByPostId(postId);
    }

    @Override
    public boolean addReply(Reply reply) {
        try {
            Date date = TimeUtil.getCurrentTimeToDate();
            reply.setReplyTime(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if(replyDao.addReply(reply)){
            int count = replyDao.findByPostId(reply.getPostId()).size();
            return postDao.updateReplyNumber(reply.getPostId(),count);
        }
        return false;
    }

    @Override
    public List<Reply> getReplyListByUserId(int userId) {
        return replyDao.findByUserId(userId);
    }


    @Override
    public boolean deleteReplyByReplyId(int replyId, int postId) {
        if(replyDao.deleteReplyById(replyId)){
            int count = replyDao.findByPostId(postId).size();
            return postDao.updateReplyNumber(postId,count);
        }
        return false;
    }
}
